package com.spukmk3me.animatorpack.pack0;

import com.spukmk3me.math.FPMath;
import com.spukmk3me.scene.Animator;
import com.spukmk3me.scene.Node;
import com.spukmk3me.util.ContinuousTimedAnimation;

/**
 *  A animator that makes scene nodes move in a zigzag line.
 *  \details It's based on TranslateAnimator.
 */
public class MultiColorAnimator extends Animator
{
    public MultiColorAnimator()
    {
        m_animation = new ContinuousTimedAnimation();
    }
    
    public void setColors( int[] argbColors )
    {
        m_colors = argbColors;
    }
    
    public int[] getColors()
    {
        return m_colors;
    }
    
    @Override
    public boolean isAnimating()
    {
        if ( getNode() == null )
            return false;

        return m_animation.isAnimating();
    }
    
    @Override
    protected void onNodePrepareToChange( Node arg0 ){}

    @Override
    protected void onNodeChanged()
    {
        updateCurrentColor();
    }

    @Override
    public void animate( int deltaTime )
    {
        m_animation.execute( deltaTime );
        updateCurrentColor();
    }
    
    public final void setAnimating( int mode, int firstIndex, int lastIndex,
        int[] times, int time, int limitParam, int[] colors, byte colorType )
    {
        m_animation.setAnimation( mode, firstIndex, lastIndex, times, time, limitParam );
        m_colors = colors;
        m_colorType = colorType;
    }
    
    private void updateCurrentColor()
    {
        int real    = m_animation.getCurrentIndex();
        int left    = m_animation.getLeftIndex();
        int right   = m_animation.getRightIndex();
        int color1  = m_colors[ left ];
        int color2  = m_colors[ right ];
        int nleft   = real - (left << 16);
        int nright  = 0x00010000 - nleft;
        int color = 0;
        
        color |= Math.min( 0x00FF0000, (FPMath.fpMul( (color1 >> 8) & 0x00FF0000, nleft ) +
            FPMath.fpMul( (color2 >> 8) & 0x00FF0000, nright )) & 0x00FF0000 ) << 8;
        color |= Math.min( 0x00FF0000, (FPMath.fpMul( color1 & 0x00FF0000, nleft ) +
            FPMath.fpMul( color2 & 0x00FF0000, nright )) & 0x00FF0000 );
        color |= Math.min( 0x00FF0000, (FPMath.fpMul( (color1 << 8) & 0x00FF0000, nleft ) +
            FPMath.fpMul( (color2 << 8) & 0x00FF0000, nright )) & 0x00FF0000 ) >> 8;
        color |= Math.min( 0x00FF0000, (FPMath.fpMul( (color1 << 16) & 0x00FF0000, nleft ) +
            FPMath.fpMul( (color2 << 16) & 0x00FF0000, nright )) & 0x00FF0000 ) >> 16;
        
        if ( m_colorType == PLANECOLOR )
            getNode().setPlaneColor( color );
        else if ( m_colorType == ADDITIONALCOLOR )
            getNode().setAdditionalColor( color );
    }
    
    public static final byte PLANECOLOR = 0;
    public static final byte ADDITIONALCOLOR = 1;
    
    private ContinuousTimedAnimation    m_animation;
    private int[]                       m_colors;
    private byte                        m_colorType;
}